package com.hbnu.system.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.PorprojectfileMapper;
import com.hbnu.system.model.entity.Porproject_registration;
import com.hbnu.system.model.entity.Porprojectfile;
import com.hbnu.system.service.IPorproject_registrationService;
import com.hbnu.system.service.IUserService;
import com.hbnu.system.service.PorprojectfileService;
import com.hbnu.system.utils.CheckRoleUtil;
import com.hbnu.system.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.BindException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * (Tranprojectfile)表服务实现类
 *
 * @author makejava
 * @since 2023-10-16 20:45:33
 */
@Service("porprojectfileService")
public class PorprojectfileServiceImpl extends ServiceImpl<PorprojectfileMapper, Porprojectfile> implements PorprojectfileService {
    @Autowired
    FileUploadUtil fileUploadUtil;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private IPorproject_registrationService projectorRegistrationService;
    @Autowired
    private CheckRoleUtil checkRoleUtil;

    @Override
    public boolean addPorprojectfile(Porprojectfile porprojectfile) {
        //查询纵向项目
        Porproject_registration project = projectorRegistrationService.getPorproject(porprojectfile.getPregistrationID());
        checkRoleUtil.checkPermissionProject(project.getProjectState());
        porprojectfile.setFiledate(LocalDateTime.now());
        return baseMapper.insert(porprojectfile)==1;
    }


    /**
     * 获取纵向项目附件
     * @param id 文件编号
     * @return 文件列表
     */
    @Override
    public List<Porprojectfile> pagePorprojectfile(String id) {
        if(StrUtil.isBlank(id)){
            throw new BaseException("纵向项目编号为空值");
        }
        List<Porprojectfile> porprojectfile = baseMapper.selectList(Wrappers.lambdaQuery(Porprojectfile.class)
                .eq(Porprojectfile::getPregistrationID,id));
        return porprojectfile;
    }

    /**
     * 删除纵向项目附件
     * @param id 文件编号
     * @return 附件列表
     */
    @Override
    public boolean deletPorprojectfile(String id) throws IOException {
        Porprojectfile one = super.getOne(Wrappers.lambdaQuery(Porprojectfile.class)
                .eq(Porprojectfile::getFileid, id));
        System.out.println("onr="+one);
        if (Objects.isNull(one)){
            throw new BaseException("文件编号有误!");
        }
        Porproject_registration project = projectorRegistrationService.getPorproject(one.getPregistrationID());
        checkRoleUtil.checkPermissionProject(project.getProjectState());

        boolean delete = fileUploadUtil.delete(one.getFileurl());
        if(!delete){
            throw new BindException("附件删除失败");
        }
        return baseMapper.deleteById(id) == 1;
    }

    /**
     * 修改纵向项目附件
     * @param porprojectfile 修改的纵向项目附件
     * @return 修改结果
     */
    @Override
    public boolean updatePorprojectfile(Porprojectfile porprojectfile) {
        Porproject_registration project = projectorRegistrationService.getPorproject(porprojectfile.getPregistrationID());
        checkRoleUtil.checkPermissionProject(project.getProjectState());
        return baseMapper.updateById(porprojectfile)==1;
    }


}
